package com;

import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.wall.WallFilter;
import com.base.interceptor.ModelNameInterceptor;
import com.base.interceptor.PublicInterceptor;
import com.fruits.common.Goods;
import com.fruits.common.GoodsImage;
import com.fruits.common.GoodsModel;
import com.fruits.common.Order;
import com.fruits.common.Shop;
import com.fruits.common.Tuan;
import com.fruits.controller.CustomerController;
import com.fruits.controller.FromWxPlatController;
import com.fruits.controller.IndexController;
import com.fruits.controller.MainController;
import com.fruits.controller.ShopController;
import com.fruits.controller.WxCustomerController;
import com.fruits.controller.WxShopController;
import com.jfinal.config.Constants;
import com.jfinal.config.Handlers;
import com.jfinal.config.Interceptors;
import com.jfinal.config.JFinalConfig;
import com.jfinal.config.Plugins;
import com.jfinal.config.Routes;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Model;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.render.FreeMarkerRender;
import com.wx.common.WxConfig;
import com.wx.controller.WxConfigController;
import com.wx.controller.WxMpController;

import freemarker.template.TemplateModelException;

/**
 * 应用配置
 * 
 * @author chexl
 * 
 * @date 2015-1-16
 * @time 下午05:25:08
 */
public class AppConfig extends JFinalConfig {

	@Override
	public void configConstant(Constants me) {
		loadPropertyFile("jdbc.properties");

	}

	@Override
	public void configRoute(Routes me) {
		me.add("/", IndexController.class);
		me.add("/pages/main", MainController.class);
		me.add("/pages/shop", ShopController.class);
		me.add("/pages/customer", CustomerController.class);
		me.add("/pages/wxconfig", WxConfigController.class);

		me.add("/mobile/wxshop", WxShopController.class);
		me.add("/mobile/wxcustomer", WxCustomerController.class);

		me.add("/wxmp", WxMpController.class);
		me.add("/fromwxplat", FromWxPlatController.class);
		// me.add("/pages/role", RoleController.class);
	}

	@Override
	public void configPlugin(Plugins me) {
		DruidPlugin dp = createDruid();
		ActiveRecordPlugin arp = createArp(dp);
		me.add(dp).add(arp);
	}

	private DruidPlugin createDruid() {
		String jdbcUrl = getProperty("jdbc.url");
		String jdbcUsername = getProperty("jdbc.username");
		String jdbcPassword = getProperty("jdbc.password");
		DruidPlugin dp = new DruidPlugin(jdbcUrl, jdbcUsername, jdbcPassword, JdbcConstants.MYSQL_DRIVER);
		dp.setValidationQuery("SELECT 1 FROM DUAL");
		addFilterToDruid(dp);
		return dp;
	}

	private void addFilterToDruid(DruidPlugin dp) {
		StatFilter statFilter = new StatFilter();
		dp.addFilter(statFilter);
		WallFilter wallFilter = new WallFilter();
		wallFilter.setDbType(JdbcConstants.MYSQL);
		dp.addFilter(wallFilter);
	}

	private ActiveRecordPlugin createArp(DruidPlugin dp) {
		ActiveRecordPlugin arp = new ActiveRecordPlugin(dp);
		arp.setShowSql(Boolean.valueOf(getProperty("jdbc.debug", "false")));
		arp.setDevMode(Boolean.valueOf(getProperty("jdbc.debug", "false")));
		arp.setDialect(new MysqlDialect());
		addTableMappingToArp(arp);
		return arp;
	}

	private void addTableMappingToArp(ActiveRecordPlugin arp) {
		arp.addMapping("tbl_shop", Shop.class);
		arp.addMapping("tbl_wxconfig", WxConfig.class);
		arp.addMapping("tbl_goods", Goods.class);
		arp.addMapping("tbl_goods_model", (Class<? extends Model<?>>) GoodsModel.class);
		arp.addMapping("tbl_goods_image", (Class<? extends Model<?>>) GoodsImage.class);
		arp.addMapping("tbl_tuan", Tuan.class);
		arp.addMapping("tbl_order", Order.class);

	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new PublicInterceptor());
		me.add(new ModelNameInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("ctx_path"));
	}

	@Override
	public void afterJFinalStart() {
		FreeMarkerRender.setProperties(loadPropertyFile("freemarker.properties"));
		try {
			if (!"".equals(JFinal.me().getContextPath())) {
				FreeMarkerRender.getConfiguration().setSharedVariable("ctx", JFinal.me().getContextPath());
			}

		} catch (TemplateModelException e) {
			e.printStackTrace();
		}
		super.afterJFinalStart();
	}

}
